const fs = require('fs');
const mime = require('mime');

function readStaticFile(res, path) {
    const type = mime.getType(path);
    console.log(path);
    if (type) {
        fs.readFile(path, (err, content) => {
            if (err) {
                res.writeHead(404, { "Content-Type": "text/plain" })
                res.end("404 - NOT FOUND")
            } else {
                res.writeHead(200, {
                    'content-type': type
                })
                res.end(content);
            }
        })
    } else {
        console.log(2);
        res.writeHead(200, {
            'content-type': "text/html;charset=utf-8"
        })
        res.end('文件夹不存在');
    }
}

module.exports = readStaticFile;